package rise.net;

import rise.comunity.UserData;
import rise.message.Login;
import rise.message.Packet;
import rise.ui.R;
import rise.ui.UserListActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Toast;

public class LoginTask extends SenderTask {

	private ProgressBar progressBar;
	private String username, password;
	Activity activity;

	public LoginTask(Activity activity, Login login, Context context,
			boolean fromRegistration) {
		super(login, context);

		if (!fromRegistration) {
			progressBar = (ProgressBar) activity
					.findViewById(R.id.accessProgressBar);
		}
		this.username = login.getUsername();
		this.password = login.getPassword();
	}

	@Override
	protected void onPreExecute() {
		if (progressBar != null)
			progressBar.setVisibility(ProgressBar.VISIBLE);
	}

	@Override
	protected Integer doInBackground(Void... params) {

		return super.doInBackground(params);
	}

	@Override
	protected void onPostExecute(Integer result) {
		if (progressBar != null) {
			progressBar.setVisibility(ProgressBar.INVISIBLE);
		}

		if (result == Packet.CONNECTION_ERROR
				|| result == Packet.UNKNOWN_PACKET) {
			super.onPostExecute(result);
		} else if (result == Login.ALREADY_LOGGED) {
			this.handleAlreadyLogged();
			exitFromApplication();
		} else if (result == Login.WRONG_PASSWORD
				|| result == Login.WRONG_USERNAME) {
			this.handleInvalidUserData();
			exitFromApplication();
		}
		if (result == Login.OK) {
			this.loginCompleted();

			Intent intent = new Intent(getContext(), UserListActivity.class);
			getContext().startActivity(intent);
			super.createSharedPreferences(username, password,
					rise.comunity.Status.ONLINE);
			UserData.createUserData(username, password,
					rise.comunity.Status.ONLINE);
			UserData.obtainUserData().setSocket(socket/*
													 * , printWriter,
													 * bufferedReader
													 */);

			this.startService();
			return;
		} else {
			ReceiverService receiverService = ReceiverService
					.obtainReceiverService();
			if(receiverService != null)
				receiverService.stopService();
		}
	}

	private void startService() {
		/*ReceiverService receiverService = ReceiverService
				.obtainReceiverService();*/
		//if (receiverService == null) {
			Intent serviceIntent = new Intent(getContext(),
					ReceiverService.class);
			serviceIntent.setAction("rise.net.ReceiverService");
			getContext().startService(serviceIntent);
		/*} else {
			receiverService.conditionalVariable.block();
			receiverService.startService();
		}*/
	}

	void handleAlreadyLogged() {
		Toast toast = Toast.makeText(getContext(),
				"L'utente ha già effettuato il login. Riprovare in seguito.",
				Toast.LENGTH_LONG);
		toast.show();
	}

	private void handleInvalidUserData() {
		Toast toast = Toast.makeText(getContext(),
				"Username e/o password errati.", Toast.LENGTH_LONG);
		toast.show();
	}

	void loginCompleted() {
		Toast toast = Toast.makeText(getContext(), "Login effettuato.",
				Toast.LENGTH_LONG);
		toast.show();
	}

}